package com.onemile.app;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onemile.app.base.BaseUI;
import com.onemile.data.DXml;
import com.onemile.entity.MessageCode;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.PubConst.Intent_Key;
import com.onemile.entity.model.EShopInfo;
import com.onemile.net.NetClient;
import com.onemile.net.NetImageLoaderM;
import com.onemile.utils.UtiDialogsFactory;

public class UIShopDetails extends BaseUI
{
   private EShopInfo shopInfo;
   private String shopId;

   private TextView txtRecommendedDishes;
   private TextView txtAddress;
   private TextView txtTel;
   private TextView txtOpenHours;
   private TextView txtPromotion;
   private TextView txtTopInfo;
   private ImageView imgAddFav;
   private ImageView imgGoShop;
   private ImageView imgBigImg;

   private void InitControls()
   {
	  txtRecommendedDishes = (TextView) findViewById(R.id.txtRecommendedDishes_shopdetails);
	  txtAddress = (TextView) findViewById(R.id.txtAddress_shopdetails);
	  txtTel = (TextView) findViewById(R.id.txtTel_shopdetails);
	  txtOpenHours = (TextView) findViewById(R.id.txtOpenHours_shopdetails);
	  txtPromotion = (TextView) findViewById(R.id.txtPromotion_shopdetails);

	  txtTopInfo = (TextView) findViewById(R.id.txtInfo_topinfobar);

	  imgAddFav = (ImageView) findViewById(R.id.imgAddFav_shopdetails);
	  imgAddFav.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)// 没登录就先登录，已经登陆就添加
		 {
			if (Session.getInstance().IsLogin())
			{
			   executeAsyncTask(PubConst.TaskName.FAVORITE_NEW);
			}
			else
			{
			   Intent intent = new Intent();
			   intent.setClass(UIShopDetails.this, UILogin.class);
			   intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY, PubConst.ActivityCode.LOGIN);
			   startActivityForResult(intent, PubConst.ActivityCode.SHOP_DETAILS);
			}
		 }
	  });
	  imgGoShop = (ImageView) findViewById(R.id.imgGoShop_shopdetails);
	  imgGoShop.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			if (Session.getInstance().GetCommodityCount() > 0 || Session.getInstance().getCommodityList().size() > 0)
			{
			   if (!shopId.equals(Session.getInstance().getCommodityList().get(0).getShopsId()))
			   {
				  UtiDialogsFactory.getInstance().ShowToastInfoShort(UIShopDetails.this, getString(R.string.shop_different_commodity));
				  return;
			   }
			}
			if (Session.getInstance().GetCommodityCount() == 0 || Session.getInstance().getCommodityList() == null)
			{
			   Session.getInstance().setCurrentShop(shopInfo);
			}
			Intent intent = new Intent();
			intent.setClass(UIShopDetails.this, UICommodityList.class);
			intent.putExtra(PubConst.Intent_Key.SHOP_ID, shopId);
			startActivity(intent);
		 }
	  });
	  imgBigImg = (ImageView) findViewById(R.id.imgBigImg_shopdetails);
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  try
	  {
		 InitControls();
		 executeAsyncTask(PubConst.TaskName.SHOP_DETAILS);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIShopDetails_onCreateOverride(savedInstanceState) ", e);
		 }
	  }
   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.shopdetails;
   }

   @Override
   public void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum)
   {
	  try
	  {
		 topTxt.setText("店铺详情");
		 shopId = getIntent().getStringExtra(PubConst.Intent_Key.SHOP_ID);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIShopDetails_SetTopToolbar(topTxt, leftImBtn, rightImgBtn, rightRay, cartNum) ", e);
		 }
	  }
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  if (taskName.equals(PubConst.TaskName.SHOP_DETAILS))
	  {
		 UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UIShopDetails.this, getString(R.string.dialog_loading));

		 NetImageLoaderM imageLoaderM = new NetImageLoaderM();
		 Drawable d = imageLoaderM.loadDrawable(imgBigImg, shopId, 3, false, new NetImageLoaderM.ImageCallback()
		 {
			@Override
			public void setImage(ImageView imageView, Drawable imageDrawable)
			{
			   if (imageDrawable != null)
			   {
				  imageView.setImageDrawable(imageDrawable);
			   }
			}
		 });
	  }
	  else if (taskName.equals(PubConst.TaskName.FAVORITE_NEW))
	  {
		 UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UIShopDetails.this, getString(R.string.dialog_uploading));
	  }
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  try
	  {
		 if (taskName != null && taskName.equals(PubConst.TaskName.SHOP_DETAILS))
		 {
			return NetClient.GetShopDetails(shopId);
		 }
		 else if (taskName != null && taskName.equals(PubConst.TaskName.FAVORITE_NEW))
		 {
			return NetClient.InsertNewFavorite(Session.getInstance().getUserInfo().getUserId(), shopId);
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIShopDetails_onAsyncTaskInBackground(taskName, params) ", e);
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  try
	  {
		 UtiDialogsFactory.getInstance().DismissLoadingDialog();
		 if (taskName.equals(PubConst.TaskName.SHOP_DETAILS))
		 {
			if (result != null)
			{
			   DXml data = (DXml) result;
			   shopInfo = new EShopInfo();
			   shopInfo.setShopsId(data.getBody().GetFieldValue("ShopsId"));
			   shopInfo.setShopsName(data.getBody().GetFieldValue("ShopsName"));
			   shopInfo.setShopsPoor(data.getBody().GetFieldValue("ShopsPoor"));
			   shopInfo.setShopsPraise(data.getBody().GetFieldValue("ShopsPraise"));
			   shopInfo.setShopsType(data.getBody().GetFieldValue("ShopsType"));
			   shopInfo.setRecommendedDishes(data.getBody().GetFieldValue("RecommendedDishes"));
			   shopInfo.setConsumption(data.getBody().GetFieldValue("Consumption"));
			   shopInfo.setShopsAddress(data.getBody().GetFieldValue("ShopsAddress"));
			   shopInfo.setShopsTel(data.getBody().GetFieldValue("ShopsTel"));
			   shopInfo.setPromotionsId(data.getBody().GetFieldValue("PromotionsId"));
			   shopInfo.setPromotionsTitle(data.getBody().GetFieldValue("PromotionsTitle"));
			   shopInfo.setOpeningHours(data.getBody().GetFieldValue("OpeningHours"));
			   shopInfo.setLowestPrice(data.getBody().GetFieldValue("LowestPrice"));
			   shopInfo.setLastLoginTime(data.getBody().GetFieldValue("LastLoginTime"));
			   shopInfo.setFreight(data.getBody().GetFieldValue("Freight"));
			   shopInfo.setConsumption(data.getBody().GetFieldValue("Consumption"));
			   shopInfo.setDeliveryDistance(data.getBody().GetFieldValue("DeliveryDistance"));
			   txtRecommendedDishes.setText(shopInfo.getRecommendedDishes());

			   txtAddress.setText(shopInfo.getShopsAddress());
			   txtOpenHours.setText(shopInfo.getOpeningHours());
			   txtPromotion.setText(shopInfo.getPromotionsTitle());
			   txtTel.setText(shopInfo.getShopsTel());

			   txtTopInfo.setText(String.format("%s元起送，%s公里以内", shopInfo.getLowestPrice(), shopInfo.getDeliveryDistance()));
			}
		 }
		 else if (taskName.equals(PubConst.TaskName.FAVORITE_NEW))
		 {
			if (result != null)
			{
			   DXml data = (DXml) result;
			   if (data.getHead().GetFieldValue("RspCode").equals(MessageCode.Success.Code))
			   {
				  UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIShopDetails.this, getString(R.string.dialog_tips), getString(R.string.shop_addfav_success), new DialogInterface.OnClickListener()
				  {
					 @Override
					 public void onClick(DialogInterface dialog, int which)
					 {
						dialog.dismiss();
					 }
				  });
			   }
			   else if (data.getHead().GetFieldValue("RspCode").equals(MessageCode.Favorite_Already_Exit.Code))
			   {
				  UtiDialogsFactory.getInstance().ShowToastInfoShort(UIShopDetails.this, data.getHead().GetFieldValue("RspMsg"));
			   }
			   else
			   {
				  UtiDialogsFactory.getInstance().ShowToastInfoShort(UIShopDetails.this, getString(R.string.shop_addfav_fail));
			   }
			}
			else
			{
			   UtiDialogsFactory.getInstance().ShowToastInfoShort(UIShopDetails.this, getString(R.string.shop_addfav_fail));
			}
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIShopDetails_onAsyncTaskEnd(taskId, taskName, params, result) ", e);
		 }
	  }
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
	  switch (requestCode)
	  {
		 case PubConst.ActivityCode.SHOP_DETAILS:
			if (resultCode == Activity.RESULT_OK)
			{
			   if (Session.getInstance().IsLogin())
			   {
				  executeAsyncTask(PubConst.TaskName.FAVORITE_NEW);
			   }
			}
			break;
	  }
   }

}
